import 'package:flutter/material.dart';
import 'package:flutter_changdian_football/pages/mine/application_setting/application_setting_controller.dart';
import 'package:flutter_changdian_football/pages/mine/user_store.dart';
import 'package:flutter_changdian_football/routers/app_routes.dart';
import 'package:flutter_changdian_football/utils/custom_constant.dart';
import 'package:flutter_changdian_football/widget/screenutil/custom_screenutil.dart';
import 'package:get/get.dart';
import 'package:get/route_manager.dart';

/// 应用设置
class ApplicationSettingPage extends StatelessWidget {
  const ApplicationSettingPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: const Color(0xffF8FAF9),
      appBar: AppBar(
        centerTitle: true,
        backgroundColor: const Color(0xffF8FAF9),
        elevation: 0,
        title: Text(
          '应用设置',
          style: TextStyle(fontSize: 16.sp, color: const Color(0xff333333)),
        ),
        iconTheme: const IconThemeData(
          color: Color(0xff333333)
        ),
      ),
      body: GetBuilder<ApplicationSettingController>(
        init: ApplicationSettingController(),
        builder: (applicationSettingController) {
          return SingleChildScrollView(
            padding: EdgeInsets.symmetric(horizontal: 17.w),
            child: Column(
              children: [
                Container(
                  width: double.infinity,
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(12.r)
                  ),
                  child: _listTileWidget(leftText: '推送设置', rightText: '', onTap: () {
                    if (UserStore.to.checkLoginPage()) {
                      Get.toNamed(AppRoutes.pushSetting);
                    }
                  }),
                ),

                SizedBox(height: 20.w,),

                Container(
                  width: double.infinity,
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(12.r)
                  ),
                  child: Column(
                    children: [
                      _listTileWidget(leftText: '用户服务协议', rightText: '', onTap: () {
                        Get.toNamed(AppRoutes.inAppWebView, parameters: {
                          'title': '用户服务协议',
                          'type': CustomConstant.TERMS_OF_SERVICE,
                        });
                      }),
                      Container(
                        width: double.infinity,
                        height: 0.5.w,
                        color: const Color(0xffE5E5E5 ),
                        margin: EdgeInsets.symmetric(horizontal: 12.w),
                      ),
                      _listTileWidget(leftText: '隐私协议', rightText: '', onTap: () {
                        Get.toNamed(AppRoutes.inAppWebView, parameters: {
                          'title': '隐私协议',
                          'type': CustomConstant.PRIVACY_AGREEMENT,
                        });
                      })
                    ],
                  ),
                ),

                SizedBox(height: 20.w,),

                Container(
                  width: double.infinity,
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(12.r)
                  ),
                  child: Column(
                    children: [
                      _listTileWidget(leftText: '清除缓存', rightText: applicationSettingController.cacheFileSize, onTap: () {
                        applicationSettingController.cleanCacheFileSize();
                      }),
                      Container(
                        width: double.infinity,
                        height: 0.5.w,
                        color: const Color(0xffE5E5E5 ),
                        margin: EdgeInsets.symmetric(horizontal: 12.w),
                      ),
                      _listTileWidget(leftText: '检查更新', rightText: '当前版本v${applicationSettingController.version}', onTap: () {
                        UserStore.to.checkVersion(true);
                      })
                    ],
                  ),
                ),

                SizedBox(height: 20.w,),


                GestureDetector(
                  behavior: HitTestBehavior.translucent,
                  onTap: () {
                    applicationSettingController.accountCancellation();
                  },
                  child: Container(
                    width: double.infinity,
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(12.r)
                    ),
                    alignment: Alignment.center,
                    padding: EdgeInsets.symmetric(vertical: 12.w),
                    child: Text('注销账号', style: TextStyle(fontSize: 15.sp, color: const Color(0xffd5595e)),),
                  ),
                ),


                SizedBox(height: 20.w,),


                GestureDetector(
                  behavior: HitTestBehavior.translucent,
                  onTap: () {
                    applicationSettingController.logOut();
                  },
                  child: Container(
                    width: double.infinity,
                    decoration: BoxDecoration(
                      color: Colors.white,
                      borderRadius: BorderRadius.circular(12.r)
                    ),
                    alignment: Alignment.center,
                    padding: EdgeInsets.symmetric(vertical: 12.w),
                    child: Text('退出登录', style: TextStyle(fontSize: 15.sp, color: const Color(0xffd5595e)),),
                  ),
                )
              ],
            ),
          );
        }
      ),
    );
  }


  _listTileWidget({
    String leftText = '',
    String rightText = '',
    bool isRight = true,
    bool isAvatar = false,
    Function? onTap
  }) {
    return GestureDetector(
      behavior: HitTestBehavior.translucent,
      onTap: () {
        onTap?.call();
      },
      child: Container(
        padding: EdgeInsets.symmetric(vertical: 12.w, horizontal: 12.w),
        child: Row(
          children: [
            SizedBox(
              child: Text(leftText, style: TextStyle(fontSize: 15.sp, color: const Color(0xff323232)),),
            ),
    
            const Spacer(),
    
            if (isAvatar == true) ...[
              Container(
                width: 44.w,
                height: 44.w,
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(44.r),
                  color: Colors.red
                ),
              )
            ] else ...[
              SizedBox(
                child: Text(rightText, style: TextStyle(fontSize: 14.sp, color: const Color(0xff999999)),),
              ),
            ],
    
            if (isRight) ...[
              SizedBox(width: 8.w,),
    
              SizedBox(
                width: 10.w,
                height: 10.w,
                child: Image.asset('assets/images/icon_arrow_grey_right.png'),
              )
            ],
          ],
        ),
      ),
    );
  }


}